#pragma once

#include <Windows.ui.xaml.media.dxinterop.h>
#include "GIFDecoder.h"

#include <ppltasks.h>

using namespace Windows::UI::Xaml::Media::Imaging;
using namespace Microsoft::WRL;
using namespace Windows::UI::Core;
using namespace Windows::System::Threading;
using namespace Windows::Foundation;
using namespace Windows::UI::Xaml;

namespace GIFPlayer{
	[Windows::Foundation::Metadata::WebHostHidden]
	public ref class GIFRenderer sealed{
		SurfaceImageSource^ CreateSurfaceImageSource();

		SurfaceImageSource^ m_surfaceImageSource;
		ComPtr<ISurfaceImageSourceNativeWithD2D> m_nativeImageSource;
		Windows::Foundation::Size m_surfaceSize;
		std::shared_ptr<GIFDecoder> m_gifDecoder;

		// Animation data
		UINT m_frames_count;
		UINT m_current_index;

		// Rendering members
		ComPtr<ID2D1Bitmap1> m_targetBitmap;
		ComPtr<ID2D1Effect> m_compositeEffect;

		// Caches
		std::vector<FrameMetadata> m_cached_frames_data;
		std::vector<ID2D1Bitmap1*> m_cached_frames;
	internal:
		GIFRenderer(GIFDecoder* decoder);
		

	public:
		GIFRenderer(Platform::String^ filename, UINT width, UINT height);
		
		void SetSurfaceSize(UINT width, UINT height);
		
		// Rendering methods
		UINT DrawCurrentFrameAndGoNext();
		UINT DrawCurrentFrameAndGoPrevious();
		UINT DrawFrame(UINT frameIndex);
		UINT DrawNextFrame();
		UINT DrawPreviousFrame();


		// Get methods
		SurfaceImageSource^ GetSurfaceImageSource() { return m_surfaceImageSource; }
	};
}